package com.yanqu.road.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.yanqu.road.utils.range.RangeInt;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SplitHelper {

	private static Logger logger = LogManager.getLogger(SplitHelper.class.getName());
	
	private SplitHelper() {
	}

	/**
	 * 解析;分隔的字符串到int数组
	 * @param str
	 * @return
	 */
	public static int[] splitToIntSemicolon(String str) {
		return splitToInt(str, "\\;");
	}


	public static List<Integer> strToIntList(String str) {
		List<Integer> list = new ArrayList<>();
		if (str != null && !str.isEmpty()) {
			int[] arr = splitToInt(str, ",");
			if (arr != null && arr.length > 0) {
				for (int i = 0; i < arr.length; i++) {
					list.add(arr[i]);
				}
			}
		}
		return list;
	}


	public static void print(int[] results) {
		String str = "";
		for (int i = 0; i < results.length; i++) {
			str += results[i] + ",";
		}
		print(str);
	}

	public static void print(String str) {
		System.err.println(str);
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		return isNum.matches();
	}

	public static int[] splitToInt(String str, String spStr) {
		if (str == null || str.trim().length() == 0) {
			return new int[0];
		}

		try {
			String[] temps = str.split(spStr);
			int len = temps.length;
			int[] results = new int[len];
			for (int i = 0; i < len; i++) {
				if (temps[i].equals("")) {
					continue;
				}
				results[i] = Integer.parseInt(temps[i].trim());
			}
			return results;
		} catch (Exception e) {
			logger.error("str :" + str, e);
			return new int[0];
		}
	}

}
